package cn.scw.project.system.service.impl;

import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.common.utils.sql.JpaUtil;
import cn.scw.project.system.domain.SysBanner;
import cn.scw.project.system.domain.netework.bean.SysBannerAddReq;
import cn.scw.project.system.mapper.SysBannerMapper;
import cn.scw.project.system.service.ISysBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * bannerService业务层处理
 *
 * @author api
 * @date 2020-05-14
 */
@Service
public class SysBannerServiceImpl implements ISysBannerService {
    @Autowired
    private SysBannerMapper sysBannerMapper;

    /**
     * 查询banner
     *
     * @param id bannerID
     * @return banner
     */
    @Override
    public SysBanner selectBannerById(Long id) {
        return sysBannerMapper.selectBannerById(id);
    }

    /**
     * 查询banner列表
     *
     * @param like like
     * @return like
     */
    @Override
    public List<SysBanner> selectBannerList(String like) {
        return sysBannerMapper.selectBannerList(JpaUtil.getLikeStr(like));
    }

    /**
     * 新增banner
     *
     * @param req banner
     * @return 结果
     */
    @Override
    public int insertBanner(SysBannerAddReq req) {
        if (StringUtils.isBlank(req.getImgUrl()))
            throw new CustomException("请上传图片");
        SysBanner banner=new SysBanner();
        ToolUtils.copyPropertiesIgnoreNull(req,banner);
        return sysBannerMapper.insertBanner(banner);
    }

    /**
     * 修改banner
     *
     * @param req banner
     * @return 结果
     */
    @Override
    public int updateBanner(SysBannerAddReq req) {
        if (StringUtils.isBlank(req.getImgUrl()))
            throw new CustomException("请上传图片");
        SysBanner banner=sysBannerMapper.selectBannerById(req.getId());
        ToolUtils.copyPropertiesIgnoreNull(req,banner);

        return sysBannerMapper.updateBanner(banner);
    }

    /**
     * 批量删除banner
     *
     * @param ids 需要删除的bannerID
     * @return 结果
     */
    @Override
    public int deleteBannerByIds(Long[] ids) {
        return sysBannerMapper.deleteBannerByIds(ids);
    }

    /**
     * 删除banner信息
     *
     * @param id bannerID
     * @return 结果
     */
    @Override
    public int deleteBannerById(Long id) {
        return sysBannerMapper.deleteBannerById(id);
    }
}
